package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);
    private Student stu = new Student();

    public final void start() {
        //Scanner sc = new Scanner(System.in);
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查询");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统！，再见！");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入。。。");
                    break;
            }
        }
    }

    public final void updateStudent() {
        String updateId;
        while (true) {
            System.out.println("请输入您要修改的学生ID:");
            updateId = sc.next();
            boolean exists = studentService.isExists(updateId);
            if (!exists) {
                System.out.println("您输入的ID不存在，请重新输入:");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();

        Student newStu = new Student();
        newStu.setId(updateId);
        newStu.setName(name);
        newStu.setName(age);
        newStu.setBirthday(birthday);

        studentService.updateStudent(updateId, newStu);
    }

    public final void deleteStudentById() {
        String delId;
        //键盘录入要删除的学生信息
        while (true) {
            System.out.println("请输入您要删除的学生ID:");
            delId = sc.next();
            //判断ID在容器中是否存在，如果不存在，则需要一直录入
            boolean exists = studentService.isExists(delId);
            if (!exists) {
                System.out.println("您输入的ID不存在，请重新输入:");
            } else {
                break;
            }
        }
        studentService.deleteStudentById(delId);
        System.out.println("删除成功！");
    }

    public final void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后再重新输入:");
            return;
        }
        System.out.println("学号\t\t姓名  \t  年龄  \t  生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());

            }
        }
    }

    public final void addStudent() {
        //键盘录入信息
        //Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学员ID:");
            id = sc.next();
            boolean flag = StudentService.isExists(id);
            boolean resuit = studentService.addStudent(stu);
            if (resuit) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }

            if (flag) {
                System.out.println("学号已被占用，请重新输入:");
            } else {
                break;
            }
        }
    }

    public abstract Student inputStudentInfo(String id);
}

